<template>
  <div>
    <div class="logo_container">
      <img src="@/assets/images/login.jpg" width="100%" height="100%"/>
      <h2 class="logo">ThunderSoft.UA</h2>
      <h2 class="name">Ukraine Finance System</h2>
    </div>
    <div class="login_box">
      <div style="height: 100px;width: 100%;background-color: rgba(48, 65, 96, 0.8);color: #FFFFFF;">
        <h1 style="padding-top: 30px;padding-left: 80px;">Welcome to Login</h1>
      </div>
      <!-- 登录表单区 -->
      <el-form ref="loginFormRef" :rules="loginFormRules" :model="loginForm" label-width="0px" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-ren">
          </el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input :type="isShow?'text':'password'" v-model="loginForm.password" prefix-icon="iconfont icon-suo">
            <i @click="show()" style="width:30px;height:30px" slot="suffix" :class="isShow?'el-icon-open':'el-icon-turn-off'"></i></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login()">Login in</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/assets/api'
import { setToken } from '@/assets/service/auth'

export default {
  data () {
    return {
      isShow: false,
      loginForm: {
        username: '',
        password: ''
      },
      loginFormRules: {
        username: [
          { required: true, message: 'please enter username', trigger: 'blur' }
        ],
        password: [
          { required: true, message: 'please enter password', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    // 绑定回车事件
    window.addEventListener('keydown', this.keyDown)
  },
  destroyed () {
    // 移除回车事件
    window.removeEventListener('keydown', this.keyDown, false)
  },
  methods: {
    changeText () {
      this.isEdit = true
    },
    show () {
      this.isShow ? this.isShow = false : this.isShow = true
    },
    // reset () {
    //   this.$refs.loginFormRef.resetFields()
    // },
    keyDown (e) {
      // 如果是回车则执行登录方法
      if (e.keyCode === 13) {
        this.login()
      }
    },
    // 登录
    login () {
      this.$refs.loginFormRef.validate(valid => {
        if (!valid) return
        var userName = this.loginForm.username.trim()
        login({
          username: userName,
          password: this.loginForm.password
        }).then(res => {
          if (res.code === 200) {
            setToken(res.data.token)
            sessionStorage.setItem('level', res.data.level)
            sessionStorage.setItem('userName', res.data.username)
            this.$router.push('/')
          } else if (res.code === 304) {
            this.$message.error('The user is not in the system, please contact the administrator to synchronize the personnel information!')
          } else {
            this.$message.error('Account or password error!')
          }
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.logo_container {
  width: 100%;
  height: 100%;
  top:0;
  z-index: -1;
  position: absolute;
  .logo {
    position: absolute;
    font-size: 100px;
    color: #FFFFFF;
    left: 4%;
    top: 12%;
  }
  .name {
    position: absolute;
    font-size: 40px;
    color: #FFFFFF;
    left: 5%;
    top: 28%;
  }
}
.login_box{
  width:400px;
  height: 350px;
  background-color: #fff;
  border-radius: 5px;
  position: absolute;
  left:25%;
  top: 60%;
  transform: translate(-50%,-50%);
}
.btn{
  width: 100%;
}
.login_container .el-button{
  width: 200px;
  margin-left:80px;
}
.login_form{
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
